package dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBConnection {
    private static String driverName;
    private static String url;
    private static String user;
    private static String password;
    static {
        InputStream in = DBConnection.class.getClassLoader().getResourceAsStream("db.properties");
        Properties pro = new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driverName= pro.getProperty("jdbc.driverName");
        url= pro.getProperty("jdbc.url");
        user= pro.getProperty("jdbc.user");
        password= pro.getProperty("jdbc.password");
    }
    public static Connection getConnection() {

        try {
            Class.forName(driverName);//加载驱动
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
        } catch (Exception e) {
            return null;
        }
    }

    public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
